var setting = {
   view:{
       selectedMulti:false,
       showLine:true
   },
   check:{
       enable:false
   },
   // async:{
	//    type:'GET',
   //     enable:true,//设置 zTree是否开启异步加载模式  加载全部信息
   //     url:sysModules+"/xtarearegion/list",//Ajax获取数据的 URL地址
   //     otherParam:{
   //  	 　　'expanded':function(){return 'true'}
   //     } //异步参数
   // },
   data:{
	   //必须使用data  
       simpleData:{
           enable:true,
           idKey:"id",//id编号命名 默认  
           pIdKey:"pId",//父id编号命名 默认   
           rootPId:0 //用于修正根节点父节点数据，即 pIdKey 指定的属性值  
       }
   },
   edit:{
       enable:false
   },  
   callback:{  
       onClick:onClick,//单击事件
       onAsyncSuccess:onAsyncSuccess//加载数据完成事件
   }  
};
$(document).ready(function(){
	InitztData();
});
//初始数据
function InitztData() {
    var zTreeNodes;
    dialogWating = showWating({msg:'正在拼命的加载中...'});
    ajaxBRequestCallFn(sysModules+"/xtAreaRegion/list",null,function(result){
        zTreeNodes = eval("(" + result.data + ")");
        treeObj = $.fn.zTree.init($("#tree"), setting,zTreeNodes);
        var zTree = $.fn.zTree.getZTreeObj("tree");
        //获取根节点个数,getNodes获取的是根节点的集合
        var nodeList = zTree.getNodes();
        //展开第一个根节点
        zTree.expandNode(nodeList[0], true);
        closeWating(null,dialogWating);
    },null,"GET");
}

//刷新
function refreshAll(){
	InitztData();
}

//加载数据完成事件
function onAsyncSuccess(event, treeId, treeNode, msg){  
	/*var length = $('#keyword').val().length;
	if(length > 0){
		var zTree = $.fn.zTree.getZTreeObj(treeId);
	    var nodeList = zTree.getNodesByParamFuzzy("name", $('#keyword').val());
	    //将找到的nodelist节点更新至Ztree内
	    $.fn.zTree.init($("#"+treeId), setting, nodeList);
	}
	closeWating(null,dialogWating);*/
}  

//单击事件
function onClick(event, treeId, treeNode, msg){
    $.ajax({
        type:"GET",
        url:sysModules+"/xtAreaRegion/get/"+treeNode.id,
        success: function(result){
            result = result.data;
            $("#d_name").val(result.NAME);
            $("#d_code").val(result.CODE);
            $("#d_short").val(result.NAME_EN);
            $("#d_jd").val(result.LONGITUDE);
            $("#d_wd").val(result.LATITUDE);
        }
    });
}  

/**
 * 搜索树，显示并展示
 * @param treeId
 * @param text文本框的id
 */
function filter(){
	InitztData();
}

//删除
function delXtAreaRegion(){
	var zTree = $.fn.zTree.getZTreeObj("tree"),
	nodes = zTree.getSelectedNodes();
	if (nodes.length != 1) {
		toastrBoot(4,"必须选择一条记录进行删除");
		return;
	}
	var params = {ID:nodes[0].id,_method:'DELETE'};
	msgTishCallFnBoot("确定要删除所选择的数据？",function(){
		ajaxBRequestCallFn(sysModules+'/xtAreaRegion/delete',params,function(result){
			try {
	    		result = eval("(" + result + ")");  
	    		result = result[0];
	    		if(typeof(result.success) != "undefined"){
	    			if(result.success){
	            		window.parent.toastrBoot(3,result.msg);
	            		refreshAll();
	        		}else{
	        			window.parent.toastrBoot(4,result.msg);
	        		}
	    		}
			} catch (e) {
				
			}
		},null,"DELETE");
	})
}